<?php
class CustomerController extends Controller {
	
	// 顾客帐号管理
	function ManageAction()
	{
		$this->view("customer.manage","item");
	}
	
    // 顾客帐号管理
	function IntentionAction()
	{
		$this->view("customer.intention","item");
	}
	
	// 顾客数据
	function CustomerListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["types"] = P("types","0");
		$p["user"] = P("user","");
		$p["name"] = P("name","");
		$p["sortField"] = P("sortField","cid");
		$p["sortOrder"] = P("sortOrder","asc");
		
		//区域
		$area = trim(P("area","1"));
		$areaList = CommonModel::getInstance()->getAreaList();
		$area_id = ArrayTool::ArrayToIDs($areaList,$area);
		$p["area"] = $area_id;
		
		$data = CustomerModel::getInstance()->getCustomer($p);
		$this->json->toJson(array("total"=>CustomerModel::getInstance()->getCount($p),"data"=>$data));
	}
	
	// 顾客(修改)
	function CustomerUpdAction(){
        //date_default_timezone_set('UTC');
		$p['cid'] = PA(P("data",array()),"cid","0");
		$p['sid'] = PA(P("data",array()),"sid","0");
		$p['level'] = PA(P("data",array()),"level","");
		$p['start_time'] = PA(P("data",array()),"st_time","");
		$p['year_num'] = PA(P("data",array()),"year_num","");

        $data = array(
            'level' => PA(P("data",array()),"level",""),
            'year_num' => PA(P("data",array()),"year_num",""),
        );
        if ($p['start_time']) {
            //echo strtotime($p['start_time']);exit;
            //echo DateTool::intToDateTime(strtotime($p['start_time']));exit;
            //$data['start_time'] = DateTool::intToDateTime(strtotime($p['start_time']));
        }
		CustomerModel::getInstance()->ICustomerUpd($data, $p['cid']);
	}
	
	// 顾客(删除)
	function CustomerDelAction(){
		$p['id'] = P("id","0");
		CustomerModel::getInstance()->CustomerDel($p);
	}
	
	// 顾客(启用)
	function StopStartAction(){
		$p['id'] = P("id","0");
		$p['stop'] = 0;
		CustomerModel::getInstance()->StopStatus($p);
		$this->json->code("OK")->out();
	}
	
	// 顾客(停用)
	function StopEndAction(){
		$p['id'] = P("id","0");
		$p['stop'] = 1;
		CustomerModel::getInstance()->StopStatus($p);
		$this->json->code("OK")->out();
	}

    /**
     * 审核客户
     */
    public function VerifyAction()
    {
        $p['type'] = P("types", 0);
        $p['id'] = P("id","0");
        CustomerModel::getInstance()->verifyCustomer($p);
        $this->json->code("OK")->out();
    }


	// 顾客(重置密码)
	function PwdReloadAction(){
		$p['id'] = P("id","0");
        $row = FrameworkModel::getInstance()->getUserById($p['id']);
		if ($row) {
			CustomerModel::getInstance()->PwdReload($p);
            $syncode = _authcode(
                serialize(array(
                        'username'=>$row["user"],
                        'password'=>md5('888888'),
                    )
                ),'ENCODE','lx_crm');
            file_get_contents(JIFEN_PATH.'/user.php?act=syn_password&syncode='.urlencode($syncode));
        }

		$this->json->code("OK")->out();
	}
	
	// 顾客(升级)
	function TypesUpAction(){
		$p['id'] = P("id","0");
		$p['types'] = 1;
		CustomerModel::getInstance()->TypesUp($p);
		$this->json->code("OK")->out();
	}
	
	// 顾客(降级)
	function TypesDownAction(){
		$p['id'] = P("id","0");
		$p['types'] = 0;
		CustomerModel::getInstance()->TypesUp($p);
		$this->json->code("OK")->out();
	}
	
	// 投诉信息处理
	function ComplaintsAction(){
		$this->view("customer.complaints","item");
	}
	
	// 投诉处理中
	function ComplaintsRunAction() {
		$p['id'] = P("id","0");
		$p['status'] = "处理中";
		ComplaintsModel::getInstance()->ComplaintsRun($p);
		$this->json->code("OK")->out();
	}
	
	// 投诉作废
	function ComplaintsDelAction() {
		$p['id'] = P("id","0");
		$p['status'] = "已作废";
		ComplaintsModel::getInstance()->ComplaintsRun($p);
		$this->json->code("OK")->out();
	}
	
	// 投诉处理结果
	function ComplaintsConAction() {
		$p["uid"]=$this->user["id"];
		$this->data["StaffInfo"]=CommonModel::getInstance()->getStaffInfo($p);
		$this->view("customer.complaints.edit","item");
	}
	
	// 保存处理结果
	function ComplaintsSaveAction() {
		$p["id"]=PA(P("data",array()),"id","0");
		$p['process_agency'] = PA(P("data",array()),"process_agency","");
		$p['process_staff'] = PA(P("data",array()),"process_staff","");
		$p['process_result'] = PA(P("data",array()),"process_result","");
		$process_time = PA(P("data",array()),"process_time","");
		$p['process_time']=DateTool::intToDateTime(strtotime($process_time)-3600*8);
		$p['blame'] = PA(P("data",array()),"blame","0");
		
		ComplaintsModel::getInstance()->ComplaintsSave($p);
		$this->json->code("OK")->out();
	}
	
	// 客户同步
	function SynchronousAction(){
		$url = JIFEN_PATH."/user.php?act=syn_register&syncode=";
		//$url = "http://localhost/lxy/user.php?act=syn_register&syncode=";

		$p["sid"] = P("id","0");
		$user = CustomerModel::getInstance()->sny($p);
		$user["email"] = (trim($user["email"])=="")?'test@126.com':trim($user["email"]);
		
        /** 需要同步的帐号 **/
        $userlist = array();
        $syncode = _authcode(
        	serialize(array(
        				'username'=>$user["users"],
                		'password'=>$user["pwd"],
                		'email'=>$user["email"]
            			)
            		),'ENCODE', 'lx_crm');//echo $url.implode('|', $syncode);exit;

        /** 返回json数据，err表示有积分商城有重复的用户名,suc表示同步成功的用户 **/
        /**
         * {"err":["user_4","user_1","user_4","user_8","user_4"],"suc":["user_11","user_12"]}
         *
         */
        $aa =  file_get_contents($url.urlencode($syncode));
        $bb =  json_decode($aa);
        if($bb->suc[0] == $user["users"]){
            CustomerModel::getInstance()->setsny($p);
            $this->json->code("OK")->out();
            return true;
        }
		$this->json->code("ng")->out();
	}
}
?>